﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Web.Security;
using SMM.MainForm;

namespace SMM
{
    public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();
            sqlHelper sqlHelper = new sqlHelper();
            if (sqlHelper.linkDatabase()) {
                linkTxt.Text = "数据库连接成功";
                linkTxt.ForeColor = System.Drawing.Color.Green;
            }
        }

        private void LoginButton_Click(object sender, EventArgs e)
        {
            sqlHelper helper = new sqlHelper();
            if (rbCashier.Checked)
            {
                if (helper.login(logIDEdit.Text, FormsAuthentication.HashPasswordForStoringInConfigFile(logPwdEdit.Text, "MD5"))&&logIDEdit.Text.Substring(0,1)=="C")
                {
                    this.Hide();
                    CashForm f = new CashForm(logIDEdit.Text);
                    f.ShowDialog();
                }
                else {
                    MessageBox.Show("请检查ID或密码！", "登陆错误");
                }
            }
            else if (rbCargoclerk.Checked)
            {
                if (helper.login(logIDEdit.Text, FormsAuthentication.HashPasswordForStoringInConfigFile(logPwdEdit.Text, "MD5")) && logIDEdit.Text.Substring(0, 1) == "G")
                {
                    this.Hide();
                    CargoForm f = new CargoForm(logIDEdit.Text);
                    f.ShowDialog();
                }
                else
                {
                    MessageBox.Show("请检查ID或密码！", "登陆错误");
                }
            }
            else if (rbAdmin.Checked)
            {
                if ("F7E0B956540676A129760A3EAE309294".Equals(FormsAuthentication.HashPasswordForStoringInConfigFile(logPwdEdit.Text, "MD5")))
                {
                    this.Hide();
                    AdminForm f = new AdminForm();
                    f.ShowDialog();
                }
                else
                {
                    MessageBox.Show("管理员密码错误！", "登陆错误");
                }
            }
        }

        private void rbCashier_Click(object sender, EventArgs e)
        {
            if (rbCashier.Checked)
            {
                logIDEdit.ReadOnly = false;
            }
            else if (rbCargoclerk.Checked)
            {
                logIDEdit.ReadOnly = false;
            }
            else if (rbAdmin.Checked)
            {
                logIDEdit.Text = "";
                logIDEdit.ReadOnly = true;
            }
        }
    }
}
